// pages/login/logs.js

const fetch = require('../../utils/util.js');
const query = fetch.fetch;
const api = fetch.api



Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:'获取验证码',
    buttondis:false,
    openid:'',
    value:'',
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  bindKeyInput:function(e){
    this.setData({
      value: e.detail.value
    })
  },
  codeInput(e){
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


//登录
  btnlogin(){
    let {value,code} = this.data;
    wx.getStorage({
            key: 'openid',
            success: function (openid) {
              wx.getStorage({
                key: 'sessionId',
                success: function (sessionId) {
                  const data = query(api.login, 'post', { phoneNumber: value, verifCode: code, openId: openid.data, 
                    sessionId: sessionId.data})
                    data.then(data=>{
                      console.log(data)
                      if (data.status == 1){
                        wx.switchTab({
                          url: '../index/index'
                        })
                      } else if (data.status == 2){
                        wx.showLoading({
                          title: '验证码不正确！！！',
                        })
                        setTimeout(function () {
                          wx.hideLoading()
                        }, 1000)
                      } else{
                        let msg = JSON.parse(data.message)
                        if (msg.phoneNumber!=null)
                          wx.showLoading({
                            title: msg.phoneNumber+","+ msg.verifCode,
                          })
                          setTimeout(function () {
                            wx.hideLoading()
                          }, 1000)
                      }
                    })
                }
              })
            }
          })
  },
  //获取验证码
   tapName: function () {
     const data = query(api.getcode, 'post', { phoneNumber: this.data.value})
     data.then(data=>{
       wx.setStorage({
         key: "sessionId",
         data: data.sessionId
       })
     })
     this.setData({
       buttondis: true
     })
     let time = 60;
     var timer=setInterval(()=>{
       time--;
       this.setData({
         time: time+"s",
       })
      if(time==0){
        this.setData({
          time: '重新发送',
          buttondis: false
        })
        clearInterval(timer)
      }
     },1000)
     
   }
})